<?php
namespace Admin\Controller;

use Think\Controller;

class GlobalController extends Controller
{

    var $auto_publish_hours = array(
        array("time"=>"05:00", "text"=>"上午5点"), 
    	array("time"=>"07:00", "text"=>"上午7点"), 
    	array("time"=>"09:00", "text"=>"上午9点"),
        array("time"=>"11:00", "text"=>"上午11点"),
    	array("time"=>"12:00", "text"=>"中午12点"),
        array("time"=>"16:00", "text"=>"下午16:00"),
    	array("time"=>"17:30", "text"=>"下午17:30"), 
    	array("time"=>"21:00", "text"=>"晚上21点"),
    );

    //基类初始化函数
    public function _initialize()
    {
        $actionName = strtolower(__ACTION__);
        $actionName = str_replace('/index.php', '', $actionName);
        $funcData = I('get.');
        $funcData = http_build_query($funcData);

        if (!in_array($actionName, array('/admin/admin/login', '/admin/admin/dologin'))) {
            //没有登录先登录
            $admin_uid = session('ADMIN_UID');
            if (empty($admin_uid)) {
                if (!IS_AJAX) {
                    redirect(U('/admin/admin/login'), 0);
                } else {
                    $this->ajaxReturn(array(), '您尚未登录', 'tip_error');
                }
            }

            //权限判断
            $ignoreArr = array(
                '/admin/admin/logout',
                '/admin/index/index',
                '/admin/admin/changepwd',
                '/admin/public/preview',
                '/admin/public/thumb',
                '/admin/index/signsina',
                '/admin/index/callback_sina',
                '/admin/weixin/to_weibo',
                '/admin/weixin/crawler_content',
            );
            $atomModel = new \Admin\Model\AdminAtomModel();
            $atomInfo = $atomModel->getAtomInfo4Url($actionName, $funcData);
            $atomId = empty($atomInfo['id']) ? 0 : $atomInfo['id'];
            $this->assign('atomId', $atomId);
            $this->assign('admin_name', session('ADMIN_UNAME'));
            if (!in_array($actionName, $ignoreArr)) {
                $userAdmin = new \Admin\Model\AdminModel();
                $atomArray = $userAdmin->getAtomArray4id($admin_uid);
                if (!array_key_exists($atomId, $atomArray)) {
                    if (IS_AJAX) {
                        $this->ajaxReturn(array(), '您没有权限使用该功能', 'tip_error');
                    } else {
                        $this->error('您没有权限使用该功能');
                    }
                }
            }
        }
    }

    public function ajaxReturn($data, $info, $status)
    {
        $arr = array(
            'data' => $data,
            'info' => $info,
            'status' => $status,
        );
        parent::ajaxReturn($arr, 'JSON');
    }

    //密码加密
    protected function encryptPassWord($passWord)
    {
        return md5(md5($passWord) . '7e80sdf0z9qd7es0i7c57392');
    }
}